<template>
  <a-modal
    title="教室编辑"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input v-decorator="['id']" />
        </a-form-item>

        <a-form-item
          label="讲师名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入讲师名称" v-decorator="['name', {rules: [{required: true, message: '请输入讲师名称！'}]}]" />
        </a-form-item>

        <a-form-item
          label="讲师介绍"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入讲师介绍" type="textarea" :rows="2" v-decorator="['description', {rules: [{required: false, message: '请输入讲师介绍！'}]}]" />
        </a-form-item>

      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import { lecturerEdit } from '@/api/modular/garden/courseManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              name: record.name,
              description: record.description,
            }
          )
        })
      },

      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true;
        validateFields((errors, values) => {
          if (!errors) {
            values.lecturerType = 0;
            lecturerEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
